package com.dap.core.config;

import java.util.Iterator;
import java.util.ServiceLoader;

import com.dap.configsvrclient.ConfigServerClient;


public class ConfigServerClientFactory {
    public static ConfigServerClient getConfigServerClient() {
        boolean isConfigServerEnabled = BootstrapConfigUtil.isTrue(BootstrapConfigUtil.CONFIG_SERVER_ENABLE);
        if (!isConfigServerEnabled) {
            throw new RuntimeException(
                "The configserver enable option must be true in the bootstrap properties! ");
        }
        ServiceLoader<ConfigServerClient> s = ServiceLoader.load(ConfigServerClient.class);
        Iterator<ConfigServerClient> it = s.iterator();
        if (it.hasNext()) {
            ConfigServerClient c = (ConfigServerClient) it.next();
            if (it.hasNext()) {
                throw new RuntimeException("There are two or more ConfigServerClient implementation! ");
            }
            return c;
        }
        throw new RuntimeException("There are no ConfigServerClient implementation! ");
    }
}
